<?php
class ControllerPaymentKlarnaInvoice extends AdminController {
    private $error = array();

    public function index() {
        
		$this->document->setTitle($this->_['heading_title']);
		
		M('setting/setting');

        if ($this->request->isPost() && $this->validate()) {
			$status = false;
			
			foreach ($this->request->post['klarna_invoice'] as $klarna_invoice) {
				if ($klarna_invoice['status']) {
					$status = true;
					
					break;
				}
			}			
			
			$data = array(
				'klarna_invoice_pclasses' => $this->pclasses,
				'klarna_invoice_status'   => $status
			);
			
			$this->model_setting_setting->editSetting('klarna_invoice', array_merge($this->request->post, $data));
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(U('extension/payment'));
        }
 		
        
        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['text_home'],
            'href'      => UA('common/home'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['text_payment'],
            'href'      => UA('extension/payment'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->_['heading_title'],
            'href'      => UA('payment/klarna_invoice'),
            'separator' => ' :: '
        );
		
        $this->data['action'] = UA('payment/klarna_invoice');
       
	    $this->data['cancel'] = UA('extension/payment');

		$this->data['countries'] = array();
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_germany'],
			'code' => 'DEU'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_netherlands'],
			'code' => 'NLD'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_denmark'],
			'code' => 'DNK'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_sweden'],
			'code' => 'SWE'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_norway'],
			'code' => 'NOR'
		);
		
		$this->data['countries'][] = array(
			'name' => $this->_['text_finland'],
			'code' => 'FIN'
		);

		if (isset($this->request->post['klarna_invoice'])) {
			$this->data['klarna_invoice'] = $this->request->post['klarna_invoice'];
		} else {
			$this->data['klarna_invoice'] = C('klarna_invoice');
		}
		
		M('localisation/geo_zone');			
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

		M('localisation/order_status');			
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
        
		$file = DIR_LOGS . 'klarna_invoice.log';
        
        if (file_exists($file)) {
            $this->data['log'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
        } else {
            $this->data['log'] = '';
        }
        
        $this->data['clear'] = UA('payment/klarna_invoice/clear');

        $this->display('payment/klarna_invoice.tpl');
    }

    private function validate() {
        if (!$this->hashMP()) {
            $this->setMessage('error_warning', $this->_['error_permission']);
            return false;
        }

        return true;
    }
    
    private function parseResponse($node, $document) {
        $child = $node;

        switch ($child->nodeName) {
            case 'string':
                $value = $child->nodeValue;
                break;

            case 'boolean':
                $value = (string) $child->nodeValue;

                if ($value == '0') {
                    $value = false;
                } elseif ($value == '1') {
                    $value = true;
                } else {
                    $value = null;
                }

                break;

            case 'integer':
            case 'int':
            case 'i4':
            case 'i8':
                $value = (int) $child->nodeValue;
                break;

            case 'array':
                $value = array();
                
                $xpath = new DOMXPath($document);
                $entries = $xpath->query('.//array/data/value', $child);
                
                for ($i = 0; $i < $entries->length; $i++) {
                    $value[] = $this->parseResponse($entries->item($i)->firstChild, $document);
                }

                break;

            default:
                $value = null;
        }

        return $value;
    }
	
    public function clear() {
        $this->language->load('payment/klarna_invoice');
		
		$file = DIR_LOGS . 'klarna_invoice.log';
		
		$handle = fopen($file, 'w+'); 
				
		fclose($handle); 
				
		$this->session->set_flashdata('success', $this->_['text_success']);
        
        $this->redirect(UA('payment/klarna_invoice'));
    }    
}
